package servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dataTypes.*;

/*
 * Gets by params to display to user
 */

@SuppressWarnings("serial")
public class getCoupons extends HttpServlet {
	
	public getCoupons() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String gName=request.getParameter("gName");
			if ((gName== null || "".equals(gName.trim()) || gName.equals("null"))&&request.getUserPrincipal()==null){
				appManager.getInstance().signIn(request, response);
			}
			else{
				if(gName== null || "".equals(gName.trim()) || gName.equals("null")){
					gName=request.getUserPrincipal().getName();
				}
			Map<deal,List<activeUserDeals>> dealsNcoupons = appManager.getInstance().getUserCoupons(gName);
			
			doResponse(response, gName, dealsNcoupons);
						
			}
		} catch (Exception e) { 
			e.printStackTrace();
		}		

	}

	private void doResponse(HttpServletResponse response, String googleName, Map<deal, List<activeUserDeals>> dealsNcoupons) throws IOException{
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Content-Encoding","UTF-8");
		
		response.getWriter().println("<html>");
		response.getWriter().println("<head>");
		response.getWriter().println("<title>CoupoNow</title>");

		response.getWriter().println("<link type='text/css' rel='stylesheet' media='screen' href='/jqtouch/themes/jqt/theme.css' charset='utf-8'>");
		response.getWriter().println("<link type='text/css' rel='stylesheet' media='screen' href='/jqtouch/jqtouch/jqtouch.css' charset='utf-8'>");
		response.getWriter().println("<script src='/jqtouch/jqtouch/jquery-1.4.2.js'></script>");
		response.getWriter().println("<script type='application/x-javascript' src='/jqtouch/jqtouch/jqtouch.js' charset='utf-8'></script>");
		response.getWriter().println("<script type='text/javascript'>");
		response.getWriter().println("var jQT =  new $.jQTouch({});");
		response.getWriter().println("</script>");

		response.getWriter().println("</head>");

		response.getWriter().println("<body>");
		response.getWriter().println("<div id=\"jqt\">");
		response.getWriter().println("<div id='home' selected='true'>");
		response.getWriter().println("<div class='toolbar'>");
		response.getWriter().println("<h1><img src='heading1.png' width='110px' height='38px'/></h1>");
		response.getWriter().println("<a href='/?username=" + googleName + "' target='_webapp' class='button leftButton'>חזור</a>");
		response.getWriter().println("</div>");
		
		response.getWriter().println("<ul id='lst'>");

		 Iterator it = dealsNcoupons.entrySet().iterator();
		    while (it.hasNext()) {
		        Map.Entry pairs = (Map.Entry)it.next();
		        double percent = ((deal) pairs.getKey()).getPercent()*100 / ((deal) pairs.getKey()).getAmount();
				response.getWriter().println("<li class='arrow' style='text-align:right;color:fff'><strong> שם עסק:  </strong>" + ((deal) pairs.getKey()).getBusiness().getBusiness() + "<strong> הנחה:  </strong>" +percent + 
						"% <img src='" + ((deal) pairs.getKey()).getBusiness().getPhotoLink() + "'height='40' width='40' style='float:right; margin-left:6px;'/><BR>" +
						"<strong> בתוקף עד:  </strong>" + ((deal) pairs.getKey()).getEnd().getHours()+ ":" + ((deal) pairs.getKey()).getEnd().getMinutes()); 
				
				for (activeUserDeals AUD : (List<activeUserDeals>)pairs.getValue()){
					response.getWriter().println("<BR><class style='text-align:center;color:fff'>" + "מספר קופון:" + AUD.getcouponcode() + "</class>");
				}
				response.getWriter().println("</li>");
		    }

		response.getWriter().println("</ul>");


		
		
		response.getWriter().println("</div>");
		response.getWriter().println("</div>");
		response.getWriter().println("</body>");
		response.getWriter().println("</html>");
	}

	
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}




